package com.practice.web.controller.user;

import com.practice.domain.user.User;
import com.practice.exception.PracticeException;
import com.practice.service.user.UserService;
import com.practice.util.PasswordUtil;
import com.practice.util.RegexUtil;
import com.practice.web.controller.BaseController;
import com.practice.web.controller.request.user.UserLoginFormBean;
import com.practice.web.controller.request.user.UserRegisterFormBean;
import com.practice.web.controller.response.user.UserDetailResponseBean;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by Administrator on 2016/8/21.
 */
@Controller
@RequestMapping(value = "/app/user", produces={"text/html;charset=UTF-8;","application/json;"})
public class AppUserController extends BaseController {
  @Autowired
  private UserService userService;

  @ResponseBody
  @RequestMapping(value = "/detail", method = RequestMethod.GET)
  public String appDetail(@RequestParam("id") Long id, Model model, HttpServletRequest request) {
    User current = getUser(request);
    try {
      User user = userService.detail(id);
      if (!user.getId().equals(current.getId())) {
        return toJsonStr(HttpStatus.OK, new UserDetailResponseBean(current));
      }
      model.addAttribute("user", new UserDetailResponseBean(user));
    } catch (PracticeException pe) {
      return toJsonStr(HttpStatus.EXPECTATION_FAILED, "");
    }
    return toJsonStr(HttpStatus.OK, new UserDetailResponseBean(current));
  }

  @RequestMapping(value = "/checkEmail", method = RequestMethod.GET)
  @ResponseBody
  public String appCheckEmail(@RequestParam("email") String email, Model model) {
    try {
      userService.checkEmail(email);
      return toJsonStr(HttpStatus.OK, "");
    } catch (PracticeException e) {
      model.addAttribute("msg", e.getMessage());
      return toJsonStr(HttpStatus.EXPECTATION_FAILED, e.getMessage());
    }
  }

  @RequestMapping(value = "/checkUsername", method = RequestMethod.GET)
  @ResponseBody
  public String checkUsername(@RequestParam("username") String username, Model model) {
    try {
      userService.checkUsername(username);
      return toJsonStr(HttpStatus.OK, "");
    } catch (PracticeException e) {
      return toJsonStr(HttpStatus.EXPECTATION_FAILED, e.getMessage());
    }
  }

  @ResponseBody
  @RequestMapping(value = "login", method = RequestMethod.POST)
  public String login(@ModelAttribute("luser") UserLoginFormBean bean, Model model, HttpServletRequest request) {
    User user;
    try {
      if (RegexUtil.checkEmail(bean.getUsername())) {
        user = userService.findByEmail(bean.getUsername());
      } else {
        user = userService.findByUsername(bean.getUsername());
      }
    } catch (PracticeException e) {
      return toJsonStr(HttpStatus.EXPECTATION_FAILED, e.getMessage());
    }

    if (null == user || PasswordUtil.checkPassword(bean.getPassword(), user.getPassword())) {
      return toJsonStr(HttpStatus.EXPECTATION_FAILED, "用户名或密码错误！");
    }

    model.addAttribute("user", new UserDetailResponseBean(user));
    saveUser(user, request);
    return toJsonStr(HttpStatus.OK, "");
  }

  @ResponseBody
  @RequestMapping(value = "/register", method = RequestMethod.POST)
  public String register(@ModelAttribute("ruser") UserRegisterFormBean bean, Model model, HttpServletRequest request) {
    try {
      User user = userService.create(bean.getUsername(), bean.getEmail(), bean.getPassword(), bean.getConfirmPassword());
      model.addAttribute("user", new UserDetailResponseBean(user));
      saveUser(user, request);
      return toJsonStr(HttpStatus.OK, "注册成功！");
    } catch (PracticeException e) {
      return toJsonStr(HttpStatus.EXPECTATION_FAILED, e.getMessage());
    }
  }
}

